package de.wbh.gwt.form.layouter.java.server;

import java.awt.Graphics;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.HeadlessException;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.Transparency;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;

import com.sun.pdfview.PDFFile;
import com.sun.pdfview.PDFPage;

public class PdfToImageConverter {

	private final String IMAGETYP = "png";

	/**
	 * Methode wandelt eine Pdf Datei in ein png um. Momentan wird nur die erste Seite der pdf Datei umgewandelt
	 * 
	 * @param directory
	 * @param file
	 * @throws IOException
	 */
	public void convert(String directory, File file) throws IOException {
		RandomAccessFile raf = new RandomAccessFile(file, "r");
		FileChannel channel = raf.getChannel();
		ByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());
		PDFFile pdffile = new PDFFile(buf);
		// zur zeit wird nur die erste Seite umgewandelt
		PDFPage page = pdffile.getPage(0);
		Rectangle rect = new Rectangle(0, 0, (int) page.getBBox().getWidth(), (int) page.getBBox().getHeight());
		Image img = page.getImage(rect.width * 2, rect.height * 2, rect, null, true, true);
		BufferedImage bImg = toBufferedImage(img);
		if (!new File(directory).exists()) {
			new File(directory).mkdirs();
		}

		String filname;
		int pos = file.getName().lastIndexOf(".");
		if (pos == -1) {
			filname = file.getName();
		} else {
			filname = file.getName().substring(0, pos);
		}

		File yourImageFile = new File(directory, filname + "." + this.IMAGETYP);
		ImageIO.write(bImg, this.IMAGETYP, yourImageFile);
	}

	private BufferedImage toBufferedImage(Image image) {
		if (image instanceof BufferedImage)
			return (BufferedImage) image;
		image = new ImageIcon(image).getImage();
		BufferedImage bimage = null;
		GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
		try {
			int transparency = Transparency.OPAQUE;
			GraphicsDevice gs = ge.getDefaultScreenDevice();
			GraphicsConfiguration gc = gs.getDefaultConfiguration();
			bimage = gc.createCompatibleImage(image.getWidth(null), image.getHeight(null), transparency);
		} catch (HeadlessException e) {
		}

		if (bimage == null) {
			int type = BufferedImage.TYPE_INT_RGB;
			bimage = new BufferedImage(image.getWidth(null), image.getHeight(null), type);
		}

		Graphics g = bimage.createGraphics();
		g.drawImage(image, 0, 0, null);
		g.dispose();
		return bimage;
	}

}
